import arch from 'arch';
import { app } from 'electron';
import { resolve } from 'path';
import Registry from 'rage-edit';
import { getStaticPath, logger } from '.';

const hive = 'HKEY_LOCAL_MACHINE';
const x64path = 'Software\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Run';
const x86path = 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run';
const path = arch() === 'x64' ? x64path : x86path;
const nameSuffix = process.env.NODE_ENV === 'production' ? '' : 'Dev';
const name = 'AutoWork' + nameSuffix;

const reg = new Registry(`${hive}\\${path}`);

export function setAutoStart(flag: boolean) {

  if (!app.isPackaged) return;
  logger.info(`开机启动`, `${hive}\\${path}`, flag, name, resolve(app.getPath('exe'), '../resources/static/autowork.bat'));
  try {
    if (flag) {
      reg.set(name, `"${resolve(app.getPath('exe'), '../resources/static/autowork.bat')}"`);
    } else {
      reg.set(name, '');
      // SetStringRegKey(hive, path, name, '');
    }
  } catch (error) {
    logger.info("自动启动报错了：", error)
  }
}
